package com.example.selflearning.controller;
import com.example.selflearning.domain.Product;
import org.springframework.http.CacheControl;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.WebRequest;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;
/**
 * Created by yangjingzhi on 2019/5/12.
 */
@RestController
@CrossOrigin
public class HttpCacheController {
    @RequestMapping(value = "sayHi")
    public ResponseEntity<String> sayHi(HttpServletResponse response) {
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        String dateTime = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter);
   /*     ZonedDateTime expiresDate = ZonedDateTime.now().withSecond(60);
        String expires = expiresDate.format(DateTimeFormatter.RFC_1123_DATE_TIME);*/
        CacheControl cacheControl = CacheControl.maxAge(30, TimeUnit.SECONDS);
        response.addHeader("selfHeader", "selfHeaderValue");
        return ResponseEntity.ok().cacheControl(cacheControl)
                /*.header(HttpHeaders.EXPIRES, expires)*/
                .body(dateTime);
    }

    @RequestMapping("/book/{id}")
    public ResponseEntity<Product> showBook(@PathVariable Long id) {
        Product book = new Product();
        Product p2 = book.findProduct(id);
        String version = String.valueOf(p2.getId() + 16525);
        ResponseEntity responseEntity = ResponseEntity
                .ok()
                .cacheControl(CacheControl.maxAge(30, TimeUnit.DAYS))
                .eTag(version) // 这里也能操作最后修改时间lastModified，只不过没有一一展示
                .body(p2);
        return responseEntity;
    }

    @RequestMapping("/book3")
    public ResponseEntity<Product> showBook3() {
        Product book = new Product();
        Product p2 = book.findProduct(23);
        String version = String.valueOf(p2.getId() + 16525);
        ResponseEntity responseEntity = ResponseEntity
                .ok()
                .cacheControl(CacheControl.maxAge(30, TimeUnit.DAYS))
                .eTag(version) // 这里也能操作最后修改时间lastModified，只不过没有一一展示
                .body(p2);
        return responseEntity;
    }

    @RequestMapping("/book2/{id}")
    ResponseEntity<Product> getProduct(@PathVariable long id, WebRequest request) {
        Product book = new Product();
        Product p2 = book.findProduct(id);
        long modificationDate = 15577489712231L;
        if (request.checkNotModified(modificationDate)) {
            return null;
        }
        return ResponseEntity.ok()
                .lastModified(modificationDate)
                .body(p2);
    }
}
